package slw.controls
{
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import slw.events.LabelButtonEvent;
	
	/**
	 * RadioButton
	 * 单选按钮类,除默认皮肤与CheckBox不同外，其它功能与CheckBox一致
	 * @author ddx<br/>
	 * 2011-7-30
	 *
	 */
	public class RadioButton extends LabelButton
	{
		//选恇宽度，高度，选框内部宽度，高度,选中符号与内圆间隔
		private var _boxWidth:int=14;
		private var _boxHeight:int=14;
		private var _innerWidth:int=10;
		private var _innerHeight:int=10;
		private var _innerGap:int=4;
		//勾号颜色，容器
		private var _tickColor:uint=0x0099CC;
		private var _tickContainer:Shape;
		
		//按钮是否选中
		private var _selected:Boolean;
		//是否自动切换选中和未选中状态
		private var _toggle:Boolean;
		//文本格式
		private var _format:TextFormat;
		/**
		 * 构造函数 
		 * @param label:String="Label" 显示文本
		 * 
		 */
		public function RadioButton(label:String="Label")
		{
			super(label);
			this.listName="RadioButton";
			
			init();
			
			this.round=0;
			this.padding=0;
			this.displayStates=buildDefaultStates();
			this.toggle=true;
			this.autoSize=true;
		}
		
		/*
		-----------------------------------
		setters getters
		-----------------------------------
		*/
		//
		/**
		 * 选中符号(勾号颜色)，默认值:0x0099CC
		 * @param value:uint
		 * 
		 */		
		public function set tickColor(value:uint):void{
			if(_tickColor==value) return;
			
			_tickColor=value;
			
			drawTick(_tickContainer,_tickColor,_innerWidth-_innerGap,_innerHeight-_innerGap);
		}
		//
		/**
		 * 选中符号(勾号颜色)，默认值:0x0099CC
		 * @param value:uint
		 * 
		 */
		public function get tickColor():uint{
			return _tickColor;
		}
		//
		/**
		 * 选框文本，默认值:"Label" 
		 * @param value:String
		 * 
		 */		
		override public function set label(value:String):void{
			if(this.label==value) return;
			super.label=value;
			
			updateDisplayList();
		}
		//
		/**
		 * 按钮是否选中，选中时与按下状态相同，当toggle=true时此属性才有效,默认值:false 
		 * @param value:Boolean
		 * 
		 */	
		override public function set selected(value:Boolean):void{
			if(_selected==value) return;
			
			_selected=value;
			
			showTick(_selected);
			
			this.dispatchEvent(new LabelButtonEvent(LabelButtonEvent.SELECTED_CHANGE,true,true,_selected,this.data));
		}
		//
		/**
		 * 按钮是否选中，选中时与按下状态相同，当toggle=true时此属性才有效,默认值:false 
		 * @param value:Boolean
		 * 
		 */	
		override public function get selected():Boolean{
			return _selected;
		}
		//
		/**
		 * 文本与图标间隔，默认值:5px
		 * @param value:Number
		 * 
		 */
		override public function set gap(value:Number):void{
			if(this.gap==value) return;
			
			super.gap=value;
			updateDisplayList();
		}
		/**
		 * autoSize=true时，文本或图标与边框的距离,默认值:5px, autoSize=false时忽略此属性
		 * @param value:Number
		 * 
		 */		
		override public function set padding(value:Number):void{
			if(this.padding==value) return;
			
			super.padding=value;
			
			updateDisplayList();
		}
		//
		/**
		 * 选框的位置，默认值:LabelButton.LEFT,可能的值为:<br/>
		 * <ul>
		 * 	<li>LabelButton.TOP 选框位于标签顶部<br/>
		 * 	<li>LabelButton.BOTTOM 选框位于标签底部<br/>
		 * 	<li>LabelButton.LEFT 选框位于标签左边<br/>
		 * 	<li>LabelButton.RIGHT 选框位于标签右边<br/>
		 * </ul> 
		 * @param value:String
		 * 
		 */
		override public function set position(value:String):void{
			if(this.position==value) return;
			
			super.position=value;
			updateDisplayList();
		}
		//
		/**
		 * 自动适应文本和图标宽高，对于此类值始终为默认值:true
		 * @param value:Boolean
		 * 
		 */		
		override public function set autoSize(value:Boolean):void{
			if(this.autoSize==value) return;
			super.autoSize=true;
			
			updateDisplayList();
		}
		//
		/**
		 * 按钮是否自动切换选中和未选中状态，对于此类值始终为默认值:true 
		 * @param value:Boolean
		 * 
		 */		
		override public function set toggle(value:Boolean):void{
			if(_toggle==value) return;
			
			_toggle=true;
			
			addListeners();
		}
		//
		/**
		 * 按钮是否自动切换选中和未选中状态，默认值:false 
		 * @param value:Boolean
		 * 
		 */
		override public function get toggle():Boolean{
			return _toggle;
		}
		//
		/**
		 * 默认文本格式，同TextField.defaultTextFormat
		 * @param value:TextFormat
		 * 
		 */		
		override public function set defaultTextFormat(value:TextFormat):void{
			if(this.defaultTextFormat==value){
				return;
			}
			super.defaultTextFormat=value;
			if(this.textField!=null)
				this.textField.defaultTextFormat=super.defaultTextFormat;
			_format=null;
			
			updateDisplayList();
		}
		//
		/**
		 * 选框图标，对于此类值始终为默认值:null
		 * @param value:*
		 * 
		 */		
		override public function set icon(value:*):void{
			
		}
		//
		/**
		 * 显示对象状态数组，数组元素应为显示对象或类字符串如:[mc1,mc2,mc3,mc4],["mc1","mc2","mc3","mc4"]，0-4索引处的元素或其实例将作为按钮4种状态显示，若数组长度大于4将被截断，默认值:[]
		 * @param value:Array
		 * 
		 */		
		override public function set displayStates(value:Array):void{
			if(this.displayStates==value) return;
			
			super.displayStates=value;
			
			updateDisplayList();
		}
		//
		/**
		 * 组件皮肤数组,数组元素为可视对象的类字符串(如:["Skin1","Skin2"]),皮肤将以其在数组中的顺序进行堆放,索引0处的皮肤位于最底层,
		 * 将skin设置为[]或null清除皮肤
		 * @param value:Array
		 * 
		 */		
		override public function set skin(value:Array):void{
			if(this.skin==value) return;
			
			super.skin=value;
			
			updateDisplayList();
		}
		/*
		-----------------------------------
		public  methods
		-----------------------------------
		*/
		//
		/**
		 * 设置标签文本格式
		 * @param format:TextFormat
		 * 
		 */		
		override public function setTextFormat(format:TextFormat):void{
			_format=format;
			
			updateDisplayList();
		}
		//
		override public function destroy():void{
			removeListeners();
			super.destroy();
		}
		/*
		-----------------------------------
		protected  methods
		-----------------------------------
		*/
		//显示状态
		override protected function showState(index:int):void{
			super.showState(index);
			
			checkoutSize();
			
			var $state:DisplayObject;
			if(this.skinTargets!=null){
				for(var i:int=0;i<this.skinTargets.length;i++){
					if(this.skinTargets[i] is MovieClip)
						MovieClip(this.skinTargets[i]).gotoAndStop(index+1);
					
					setWH(this.skinTargets[i]);
				}
			}
			if(this.displayStates!=null){
				$state=this.displayStates[index];
				setWH($state);
			}
			
			if(_tickContainer!=null)
				this.buildContainer.setChildIndex(_tickContainer,this.buildContainer.numChildren-1);
		}
		/*
		-----------------------------------
		private  methods
		-----------------------------------
		*/
		//初始化
		private function init():void{
			_tickContainer=new Shape();
			this.buildContainer.addChild(_tickContainer);
			
			drawTick(_tickContainer,_tickColor,_innerWidth-_innerGap,_innerHeight-_innerGap);
			
			showTick(false);
		}
		//添加侦听
		private function addListeners():void{
			this.addEventListener(MouseEvent.CLICK,thisClick);
		}
		//删除侦听
		private function removeListeners():void{
			this.removeEventListener(MouseEvent.CLICK,thisClick);
		}
		//点击
		private function thisClick(e:MouseEvent):void{
			this.selected=!_selected;
		}
		//更新显示
		private function updateDisplayList():void{
			if(this.textField!=null){
				this.textField.text=this.label==null?"":this.label;
				
				if(_format!=null)
					this.textField.setTextFormat(_format);
			}
			
			checkoutSize();
			
			if(this.textField!=null){
				this.textField.x=this.padding;
				this.textField.y=this.padding;
			}
			if(this.stateContainer!=null){
				this.stateContainer.x=this.padding;
				this.stateContainer.y=this.padding;
			}
			
			var $target:DisplayObject;
			if(this.stateContainer!=null){
				$target=this.stateContainer;
			}else{
				$target=getState(0x0,[0xffffff,0xffffff],_boxWidth,_boxHeight,this.round);
			}
			if(this.textField!=null&&$target){
				switch(this.position){
					case LabelButton.TOP:
						this.textField.y=$target.y+$target.height+this.gap;
						layoutX(this.textField,$target);
						break;
					case LabelButton.BOTTOM:
						$target.y=this.textField.y+this.textField.height+this.gap;
						layoutX(this.textField,$target);
						break;
					case LabelButton.LEFT:
						this.textField.x=$target.x+$target.width+this.gap;
						layoutY(this.textField,$target);
						break;
					case LabelButton.RIGHT:
						$target.x=this.textField.x+this.textField.width+this.gap;
						layoutY(this.textField,$target);
						break;
				}
			}
			if(_tickContainer!=null&&$target!=null){
				_tickContainer.x=$target.x+(_boxWidth-_innerWidth)/2+(_innerGap)/2+0.5;
				_tickContainer.y=$target.y+(_boxHeight-_innerHeight)/2+(_innerGap)/2;
				if(this.skinContainer!=null){
					this.skinContainer.x=$target.x;
					this.skinContainer.y=$target.y;
				}
			}
			this.setState(this.state);
		}
		//构建默认皮肤
		private function buildDefaultStates():Array{
			var $states:Array=[];
			var $borderColors:Array=[0x999999,0x0099CC,0x009999,0x999999];
			var $colors:Array=[[0xffffff,0xdddddd],[0xffffff,0x85C9E0],[0x85C9E0,0xffffff],[0xdddddd,0xffffff]];
			for(var i:int=0;i<4;i++){
				$states.push(getState($borderColors[i],$colors[i],_boxWidth,_boxHeight,this.round));
			}
			
			return $states;
		}
		//获取皮肤
		private function getState(borderColor:uint,colors:Array,width:Number,height:Number,round:Number):DisplayObject{
			var $matrix:Matrix=new Matrix();
			$matrix.createGradientBox(width,height,Math.PI/2);
			
			var $shape:Shape=new Shape();
			$shape.graphics.lineStyle(0,borderColor,1,true,"none","round");
			$shape.graphics.beginGradientFill(GradientType.RADIAL,colors,[1,1],[0,0xff],$matrix);
			$shape.graphics.drawCircle(width/2,width/2,width/2);
			$shape.graphics.endFill();
			
			/*var $ori:Number=width/2;
			var $gapX:Number=(_boxWidth-_innerWidth)/2;
			var $gapY:Number=(_boxHeight-_innerHeight)/2;
			width-=$gapX*2;
			height-=$gapY*2;
			
			$matrix=new Matrix();
			$matrix.createGradientBox(width,height,Math.PI/2);
			
			$shape.graphics.lineStyle(0,borderColor,0.5,true,"none","round");
			$shape.graphics.beginFill(0xffffff,1);
			$shape.graphics.drawCircle($ori+0.1,$ori+0.1,width/2);
			$shape.graphics.endFill();*/
			
			return $shape;
		}
		//绘制勾号
		private function drawTick(target:Shape,color:uint,drawWidth:Number,drawHeight:Number):void{
			target.graphics.clear();
			target.graphics.beginFill(color,1);
			target.graphics.drawCircle(drawWidth/2,drawWidth/2,drawWidth/2);
			target.graphics.endFill();
		}
		//显示隐藏勾号
		private function showTick(visible:Boolean):void{
			_tickContainer.visible=visible;
		}
		//x轴对齐,所有元素在x轴上对齐
		private function layoutX(...args):void{
			for(var i:int=0;i<args.length;i++){
				if(args[i].width<this.width){
					args[i].x=(this.width-args[i].width)/2;
				}
			}
		}
		//y轴对齐,所有元素在y轴上对齐
		private function layoutY(...args):void{
			for(var i:int=0;i<args.length;i++){
				if(args[i].height<this.height){
					args[i].y=(this.height-args[i].height)/2;
				}
			}
		}
		//y轴对齐,所有元素高度相加后，作为一个整体在y轴上对齐
		private function layoutYAsOne(...args):void{
			var $height:Number=0;
			for(var i:int=0;i<args.length;i++){
				$height+=DisplayObject(args[i]).height+this.gap;
			}
			if(args.length>0) $height-=this.gap;
			
			if(args[0]!=null) DisplayObject(args[0]).y=(this.height-$height)/2;
			
			for(var j:int=1;j<args.length;j++){
				DisplayObject(args[j]).y=DisplayObject(args[j-1]).y+DisplayObject(args[j-1]).height+this.gap;
			}
		}
		//x轴对齐,所有元素宽度相加后，作为一个整体在y轴上对齐
		private function layoutXAsOne(...args):void{
			var $width:Number=0;
			for(var i:int=0;i<args.length;i++){
				$width+=DisplayObject(args[i]).width+this.gap;
			}
			if(args.length>0) $width-=this.gap;
			
			if(args[0]!=null) DisplayObject(args[0]).x=(this.width-$width)/2;
			
			for(var j:int=1;j<args.length;j++){
				DisplayObject(args[j]).x=DisplayObject(args[j-1]).x+DisplayObject(args[j-1]).width+this.gap;
			}
		}
		//目标宽高
		private function setWH(target:DisplayObject):void{
			if(target!=null){
				target.width=_boxWidth;
				target.height=_boxHeight;
			}
		}
		//检测高宽
		private function checkoutSize():void{
			var $width:Number=this.padding*2;
			var $height:Number=this.padding*2;
			
			if(this.textField!=null){
				switch(this.position){
					case LabelButton.TOP:
					case LabelButton.BOTTOM:
						$width+=Math.max(_boxWidth,this.textField.width);
						$height+=_boxHeight+this.gap+this.textField.height;
						break;
					case LabelButton.LEFT:
					case LabelButton.RIGHT:
						$width+=_boxWidth+this.gap+this.textField.width;
						$height+=Math.max(_boxHeight,this.textField.height);
						break;
				}
			}else if(this.textField!=null){
				$width+=this.textField.width;
				$height+=this.textField.height;
			}else{
				$width+=_boxWidth;
				$height+=_boxHeight;
			}
			
			this.width=$width;
			this.height=$height;
		}
	}
}